package cn.tedu.pojo;

import cn.tedu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;

public class PeopleExample {
    private PeopleService peopleService;
    private People people;

    @RequestMapping("/students")
    public ModelAndView getCourses(@RequestParam(value = "studentId", required = false) Integer studentId,
                                   @RequestParam(value="startPage", required=false, defaultValue="1") Integer startPage,
                                   @RequestParam(value="pageShow", required=false, defaultValue="10") Integer pageShow ) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/students");

        //查询条件处理
        People student = new People();
        if (studentId != null)
            student.setId(studentId);

        Map<String, Object> map = new HashMap<String, Object>();
        //计算当前查询起始数据索引
        int startIndex = (startPage-1) * pageShow;
        map.put("student", student);
        map.put("startIndex", startIndex);
        map.put("pageShow", pageShow);

        //获取学生总量
        int studentTotal = peopleService.getStudentTotal();
        //计算总页数
        int pageTotal = 1;
        if (studentTotal % pageShow == 0)
            pageTotal = studentTotal / pageShow;
        else
            pageTotal = studentTotal / pageShow + 1;
        model.addObject("pageTotal", pageTotal);
        model.addObject("pageNow", startPage);

        return model;
    }



    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/exit")
    public String studentClearLogin(HttpSession session) {
        People people = (People) session.getAttribute("loginStudent");
        session.removeAttribute("loginStudent");
        return "redirect:index.jsp";
    }

    @RequestMapping("/validateAccount")
    public void validateRegisterAccount(@RequestParam("studentAccount") String studentAccount,
                                        HttpServletResponse response) throws IOException {

    }
}